<?php
namespace app\web\model;


use app\lib\exception\BaseException;
use app\lib\model\Price;

/**
 * 店铺
 * @package app\basic\model
 */
class Shop extends Common 
{
    protected function getPeriodTimeAttr($v)
    {
        return $v == 0 ? '无限期' : date('Y-m-d H:i',$v);
    }
    protected function getSubTimeAttr($v){
        return $v == 0 ? '' : date('Y-m-d',$v);
    }
    protected function setCityAttr($v,$data)
    {
        if($v == '市辖区' || $v == '县') return $data['province'];
        return $v;
    }

    public static function getBusiness($business_start,$business_end,$date,$arr){
        $item = $business_start;
        $start = strtotime($date . ' ' . $item);
        $arr[] = ['start_time'=>$item,'status'=>999,'id'=>0];
        if($start+1800 < strtotime($date . ' ' . $business_end)){
            return self::getBusiness(date('H:i',$start+1800),$business_end,$date,$arr);
        }
        return $arr;
    }
    /**
     * 获取店铺好评率
     * [getPraiseRate description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getPraiseRate($id)
    {
        $star = Comment::where(['shop_id'=>$id,'status'=>1,'is_star'=>2])->count('id');
        $tot_star = Comment::where(['shop_id'=>$id,'status'=>1])->count('id');
        if($tot_star == 0) return 0;
        return Price::pricecalc(Price::pricecalc($star,'/',$tot_star),'*',100,0);
    }
    /**
     * 店铺退货率
     * [getRefundRate description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getRefundRate($id)
    {
        $tot_order = Order::where(['shop_id'=>$id])->count('id');
        $refund_order = Order::where(['shop_id'=>$id,'status'=>5])->count('id');
        if($tot_order == 0) return 0;
        return Price::pricecalc(Price::pricecalc($refund_order,'/',$tot_order),'*',100,0);
    }
    /**
     * 接单率
     * [getReceivingRate description]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getReceivingRate($id)
    {
        $deal_dat = Config::getConfig('deal_dat');
        $t = time() - 86400 * $deal_dat;
        $tot_order = Order::where('create_time','>',$t)->count('id');
        $shop_order = Order::where([['shop_id','=',$id],['create_time','>',$t]])->count('id');
        if($tot_order == 0) return 0;
        return Price::pricecalc(Price::pricecalc($shop_order,'/',$tot_order),'*',100,0);
    }
    public static function dealOrder($id){
        return Order::where(['shop_id'=>$id,'status'=>4])->count('id');
    }
}